'''
监听80端口的debug服务器，先在pycharm或者eclipse里面debug启动此脚本(即if main的部分)，再执行发变量过来的客户端函数
'''
from tornado.tcpserver import TCPServer
from tornado.ioloop import IOLoop
import pickle

try:
	import pydevd
except:
	pass

class debugging_server(TCPServer):
	def __init__(self, listening_port):
		super(debugging_server, self).__init__()
		self.callback_entry=debugging_context
		self.listen(listening_port)
		pass
	
	def handle_stream(self, stream, address):
		'''客户端连入时触发的回调'''
		print(address.__repr__(),"connected in for debug")
		stream.read_until_close(self.callback_entry)
		pass
	pass

def debugging_context(parameter_in):
	'''debug上下文，传入的变量在此，可以断点打这里面'''
	try:
		variable_debugged=pickle.loads(parameter_in)#此为还原的变量
		pydevd.settrace(suspend=True, trace_only_current_thread=True)
	except Exception as current_error:
		print(current_error.__repr__())
		pass
	pass

if __name__ == '__main__':
	current_server=debugging_server(80)
	IOLoop.current().start()
	pass

'''以下debug客户端代码'''

import socket

def remote_debug_this_variable(debug_server, object_to_debug):
	'''向远程服务器单次发送对象'''
	
	debug_server_info=debug_server.split(':')
	server_tuple=(debug_server_info[0], int(debug_server_info[1]))
	pickled_string=pickle.dumps(object_to_debug)
	current_socket=socket.socket(socket.AF_INET, socket.SOCK_STREAM)
	current_socket.connect(server_tuple)
	current_socket.sendall(pickled_string.encode())
	current_socket.close()
	pass

'''
用例：
security='000001.SZ'#平安银行股票代码
target_string=','.join([security]*200)
return_value=remote_debug_this_variable("127.0.0.1:80", target_string)#填入debug服务器的IP和端口
'''